import sys

import core, resources_, images, output
"""
从stdin读入resources, arguments 和 output
"""
def get_arguments() -> tuple:
    data = []
    while True:
        data.append(sys.stdin.readline().strip(" \r\n"))
        if len(data) >= 3: break
    return tuple(data)

def run(resources:str, arguments:str, output_dir:str) -> None:
    try:
        font_zh = resources_.get_font(resources, "Noto_Serif_SC")
        font_en = resources_.get_font(resources, "HEAVYWEI")
    except core.AppError as e:
        print("fatal_lang_error")
        return
    
    error_queue = []
    cards = resources_.get_cards_data(arguments, error_queue)

    for load_error in error_queue:
        print(load_error)

    for card in cards:
        try:
            front, back = output.create(resources_.load_card(resources, card[0], card[1], font_zh, font_en))
        except core.AppError as e:
            print("card_error:{}:{}".format(card[0].id,e.id))
        else:
            output.output(output_dir,card[0].id,front,back)
            print("card_success:{}".format(card[0].id))

if __name__ == "__main__":
    try:
        run(*get_arguments())
    except KeyboardInterrupt:
        pass